package cn.iocoder.yudao.module.game.service.rechargerecord;

import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.game.controller.admin.rechargerecord.vo.*;
import cn.iocoder.yudao.module.game.dal.dataobject.rechargerecord.RechargeRecordDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.game.dal.mysql.rechargerecord.RechargeRecordMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.game.enums.ErrorCodeConstants.*;

/**
 * 充值记录 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class RechargeRecordServiceImpl implements RechargeRecordService {

    @Resource
    private RechargeRecordMapper rechargeRecordMapper;

    @Override
    public RechargeRecordDO createRechargeRecord(RechargeRecordSaveReqVO createReqVO) {
        // 插入
        RechargeRecordDO rechargeRecord = BeanUtils.toBean(createReqVO, RechargeRecordDO.class);
        rechargeRecordMapper.insert(rechargeRecord);
        // 返回
        return getRechargeRecord(rechargeRecord.getId());
    }

    @Override
    public void updateRechargeRecord(RechargeRecordSaveReqVO updateReqVO) {
        // 校验存在
        validateRechargeRecordExists(updateReqVO.getId());
        // 更新
        RechargeRecordDO updateObj = BeanUtils.toBean(updateReqVO, RechargeRecordDO.class);
        rechargeRecordMapper.updateById(updateObj);
    }

    @Override
    public void updateRechargeRecordByOrderId(RechargeRecordSaveReqVO updateReqVO) {
        RechargeRecordDO updateObj = BeanUtils.toBean(updateReqVO, RechargeRecordDO.class);
        rechargeRecordMapper.update(updateObj, new LambdaQueryWrapper<RechargeRecordDO>()
                .eq(RechargeRecordDO::getSn, updateReqVO.getSn()));
    }

    @Override
    public void deleteRechargeRecord(Long id) {
        // 校验存在
        validateRechargeRecordExists(id);
        // 删除
        rechargeRecordMapper.deleteById(id);
    }

    private void validateRechargeRecordExists(Long id) {
        if (rechargeRecordMapper.selectById(id) == null) {
            throw exception(RECHARGE_RECORD_NOT_EXISTS);
        }
    }

    @Override
    public RechargeRecordDO getRechargeRecord(Long id) {
        return rechargeRecordMapper.selectById(id);
    }

    @Override
    public RechargeRecordDO getRechargeRecordByOrderId(String sn) {
        return rechargeRecordMapper.selectOne(new LambdaQueryWrapper<RechargeRecordDO>()
                .eq(RechargeRecordDO::getSn, sn));
    }

    @Override
    public PageResult<RechargeRecordDO> getRechargeRecordPage(RechargeRecordPageReqVO pageReqVO) {
        return rechargeRecordMapper.selectPage(pageReqVO);
    }

    @Override
    public List<RechargeRecordDO> list(RechargeRecordPageReqVO pageReqVO) {
        return rechargeRecordMapper.selectList(new LambdaQueryWrapper<RechargeRecordDO>()
                .eq(RechargeRecordDO::getMemberId, pageReqVO.getMemberId()));
    }

}